//
//  MM_BaseViewController.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/11.
//

#import "MM_BaseViewController.h"
#import <MBProgressHUD.h>
#import "AppDelegate.h"

@interface MM_BaseViewController ()<UIGestureRecognizerDelegate,MM_TitleViewDelegate,MBProgressHUDDelegate> {
    
    MBProgressHUD *stateHud;
}
@end

@implementation MM_BaseViewController

/**
 初始化
 
 @return self
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        //默认隐藏tabbar
        self.beHideTabbar = YES;
        //默认可以侧滑返回
        self.bePopGesture = YES;
    }
    return self;
}

#pragma mark ----------------    生命周期      ------------------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    // 隐藏navBar
    self.navigationController.navigationBarHidden = YES;
    // 设置侧滑返回手势代理
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    // 设置底部颜色
    self.view.backgroundColor = [ColorUtil colorWithHexadecimal:@"#f7f7f7"];
//    // 设置状态栏
    
    // 添加自定义导航栏
    [self.view addSubview:self.titleView];
    // 判断自定义导航栏返回按钮的显示
    
    //如果是跟试图右侧返回就不要了
    if (self.navigationController.childViewControllers.count <= 1) {
        
        self.titleView.isHidebtnBack = YES;
        
    }else {
        
        self.titleView.isHidebtnBack = NO;
    }
    
    [self.tabBarController.tabBar setTintColor:[ColorUtil colorWithHexadecimal:@"#333333"]];

    // IOS11适配
    //防止tableview和scollview向下漂移
    if (@available(iOS 11.0, *)) {
        
        [UITableView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //防止tableview的sectionHeader,sectionFooter高度与设置不符
    [UITableView appearance].estimatedRowHeight = 0;
    [UITableView appearance].estimatedSectionHeaderHeight = 0;
    [UITableView appearance].estimatedSectionFooterHeight = 0;
    
    [self loadUpNetWorking];
    
    
}
//收到系统内存警告
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    /* 清空 sdWebImage的缓存 **/
    [[SDImageCache sharedImageCache]clearDiskOnCompletion:nil];
    [[SDImageCache sharedImageCache]clearMemory];
    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // titleView 最上面
    [self.view bringSubviewToFront:self.titleView];
}

#pragma mark -------- 公有方法 ------------
- (void)loadUpNetWorking {
    
//    if ([self setUpNetworkMonitoring]) {
//        [self.view hiddenEmptyView];
//        [self dealNetworkingSuccess];
//    }else {
//        
//        WS(weakSelf);
//        [self.view showEmptyViewWithImageName:@"pic_default_nonetwork" desText:@"网络连接失败" detailText:@"重新加载" offSet:100 detailClick:^{
//            [weakSelf loadUpNetWorking];
//        }];
//        
//        [self.view bringSubviewToFront:self.titleView];
//    }
}
///** 网络监听 **/
//- (BOOL)setUpNetworkMonitoring {
//
//    BOOL isReachable = [AppDelegate appDelegate].isReachable;
//    if (isReachable) {
//        return YES;
//    }else {
//        return NO;
//    }
//}

/**
 处理网络正常情况
 */
- (void)dealNetworkingSuccess{
    
}
//默认只提示文字
- (void)showStateText:(NSString *)text {
//    if ([JH_ErrorCodeTool isShowForbidHint:text]) {
//        return;
//    }
    [self showPromptTextUIWithPromptText:text title:nil andDuration:2.0];
}
- (void)showPromptTextUIWithPromptText:(NSString *)promptText
                                 title:(NSString*)title
                           andDuration:(NSTimeInterval)interval
{
    
    if (interval == 0) {
        interval = 2;
    }
    if (!promptText.length) {
        promptText = @"加载中";
    }
    if (!stateHud) {
        stateHud = [[MBProgressHUD alloc] initWithView:self.view];
        stateHud.delegate = self;
        [self.view addSubview:stateHud];
    }
    stateHud.mode = MBProgressHUDModeText;
    stateHud.detailsLabel.text = promptText;
    stateHud.detailsLabel.font = [UIFont systemFontOfSize:13.0f];
    [stateHud showAnimated:YES];
    [stateHud hideAnimated:YES afterDelay:interval];
    
}
#pragma mark - 网络加载UI
- (void)showLoadingUI
{
    [self showHudInView:self.view hint:@""];
    
}
- (void)hideLoadingUI
{
    [self hideHud];
}
#pragma mark ----------------    代理      ------------------

/**
 侧滑代理
 
 @param gestureRecognizer 手势
 @return YES，可以侧滑，NO禁止侧滑
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.navigationController.childViewControllers.count == 1) {
        return NO;
    }else{
        return self.bePopGesture;
    }
}

#pragma mark -- 标题代理 --
- (void)MM_TitleView:(MM_TitleView *)titleView clickBackButton:(UIButton *)backbtn{
    if (self.navigationController.topViewController == self)
    {
        if ([self.tabBarController isKindOfClass:NSClassFromString(@"JH_SellManageTabBarController")] && self.navigationController.childViewControllers.count == 1) {
            [self.tabBarController.navigationController popViewControllerAnimated:YES];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma mark ----------------    私有方法      ------------------
/**
 设置是否隐藏tabbar
 
 @param beHideTabbar 是否隐藏
 */
- (void)setBeHideTabbar:(BOOL)beHideTabbar{
    _beHideTabbar = beHideTabbar;
    self.hidesBottomBarWhenPushed = beHideTabbar;
}


#pragma mark -- 懒加载 --
- (MM_TitleView *)titleView{
    if (!_titleView) {
        _titleView = [[MM_TitleView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, NaviBarHeight)];
        _titleView.delegate = self;
    }
    return _titleView;
}
@end
